﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domain.Model.Repositories;
using Domain.Model.Vehiculos;
using Domain.Model.Base;
using Infrastructure.Base.Persistence;

namespace Infrastructure.Base.InDatabase
{

    public class BVehiculoRepository : VehiculoRepository
    {
        private List< Vehiculo> lista;

        public BVehiculoRepository()
        {
            this.lista = new List< Vehiculo>();

        }

        #region Métodos Heredados Generales

        public override void Insert(Vehiculo entity)
        {
            ContextoInfraccion contexto = new ContextoInfraccion();
            TblVehiculo obj = new TblVehiculo();
            
            //Identidad
            obj.id = entity.Identity.Value.ToString();
            
            //Llaves Foráneas

            //Campos Propios
            obj.vin = entity.Vin;  
            obj.placa = entity.Placa;
            obj.licenciaVehiculo = entity.LicenciaVehiculo;
            obj.tipo = entity.Tipo;
            obj.servicio = entity.Servicio;

            contexto.AddToTblVehiculo(obj);
            contexto.SaveChanges();
            contexto.Dispose();
        }

        public override Vehiculo FromId(ObjectIdentity id)
        {
            String idString = id.Value.ToString();

            ContextoInfraccion contexto = new ContextoInfraccion();
            TblVehiculo item = contexto.TblVehiculo.Where(V => V.id == idString).FirstOrDefault();
            contexto.Dispose();

            if (item != null)
            {
                Vehiculo oVehiculo = new Vehiculo(new VehiculoId(item.id), item.vin, item.placa, item.licenciaVehiculo, item.tipo, item.servicio);

                //Asignar la llave primaria de la BD
                oVehiculo.PK = item.pk_vehiculo;

                return oVehiculo;
            }

            return null;
        }

        public override ObjectIdentity NextIdentity()
        {
            return new VehiculoId(Guid.NewGuid().ToString().ToUpper());
        }

        public override void Update(Vehiculo entity)
        {
            String idString = entity.Identity.Value.ToString();
            ContextoInfraccion contexto = new ContextoInfraccion();

            TblVehiculo obj = contexto.TblVehiculo.Where(I => I.id == idString).FirstOrDefault();

            //Identidad
            obj.id = entity.Identity.Value.ToString();

            //Llaves Foráneas

            //Campos Propios
            obj.vin = entity.Vin;
            obj.placa = entity.Placa;
            obj.licenciaVehiculo = entity.LicenciaVehiculo;
            obj.tipo = entity.Tipo;
            obj.servicio = entity.Servicio;

            //Objetos de Valor


            contexto.SaveChanges();
            contexto.Dispose();
        }

        public override void Remove(ObjectIdentity id)
        {
            String idString = id.Value.ToString();
            ContextoInfraccion contexto = new ContextoInfraccion();
            
            TblVehiculo obj = contexto.TblVehiculo.Where(I => I.id == idString).FirstOrDefault();

            contexto.DeleteObject(obj);
            contexto.SaveChanges();
            contexto.Dispose();
        }

        public override List<Vehiculo> All()
        {
            ContextoInfraccion contexto = new ContextoInfraccion();
            List<TblVehiculo> listaBD = new List<TblVehiculo>();

            listaBD = contexto.TblVehiculo.OrderBy(A => A.placa).ToList();


            foreach (var item in listaBD)
            {
                Vehiculo obj = new Vehiculo(new VehiculoId(item.id), item.vin, item.placa, item.licenciaVehiculo, item.tipo, item.servicio);
                lista.Add(obj);
            }

            contexto.Dispose();
            return lista;
        }

        public override int Count()
        {
            return All().Count;
        }
        #endregion

        #region Métodos de Base de Datos
        //internal int PKFromId(ObjectIdentity objectIdentity)
        //{
        //    ContextoInfraccion contexto = new ContextoInfraccion();

        //    TblVehiculo obj = contexto.TblVehiculo.Where(I => I.id == objectIdentity.ToString()).FirstOrDefault();
            
        //    contexto.Dispose();
        //    return obj.pk_vehiculo;
        //}
        #endregion
    }
}
